package com.zimmur.controller;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.sql.visitor.functions.Now;
import com.mysql.fabric.xmlrpc.base.Array;
import com.zimmur.common.ReturnJsonEntity;
import com.zimmur.common.StatusCodeEnum;
import com.zimmur.model.account.UserCommentDetail;
import com.zimmur.service.account.IUserCommentDetailService;
import com.zimmur.service.account.IUserCommentService;
import com.zimmur.tools.PageUtil;

@RestController
@RequestMapping("/restful/userComment") 
public class UserCommentController extends BaseController{
	private static Logger log = LoggerFactory.getLogger(UserCommentController.class);
	@Resource
	private IUserCommentDetailService userCommentDetailService;
	
	@Value("#{spingMvcProperties['System.GuestId']}")
	 private String guessUserId;
	/**
	 * 添加留言
	 * @return
	 */
	@RequestMapping(value="/addComment",method=RequestMethod.POST)
	public ReturnJsonEntity addComent(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		//游客身份验证
		if(String.valueOf(getTokenUserId()).equals(guessUserId)){
			statusCodeEnum=StatusCodeEnum.CODE100200;
			entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
		}
		int commentTypeId=getParam("typeId", 1);
		String content=getParam("content");
		String images=getParam("images");
		UserCommentDetail userCommentDetail=new UserCommentDetail();
		userCommentDetail.setContent(content);
		userCommentDetail.setImages(images);
		userCommentDetail.setCommentTypeId(commentTypeId);
		userCommentDetail.setUserId(getTokenUserId());
		entity.setData(new ArrayList<>());
		try{
				int flag = userCommentDetailService.addCommentDetail(userCommentDetail);
				if(flag==0){
					statusCodeEnum = StatusCodeEnum.CODE100000;
				}else{
					statusCodeEnum = StatusCodeEnum.CODE000000;
				}
		}catch(Exception e){
			log.error(e.getMessage());
			statusCodeEnum = StatusCodeEnum.CODE100000;
		}
		
		entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
	}
	@RequestMapping(value="/commentPage",method=RequestMethod.GET)
	public ReturnJsonEntity commentPage(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		Integer pageNow= getIntParam("pageNow");
		long userId=getTokenUserId();
		//游客身份验证
		if(String.valueOf(userId).equals(guessUserId)){
			statusCodeEnum=StatusCodeEnum.CODE100200;
			entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
		}
		try{
				PageUtil< UserCommentDetail> page=userCommentDetailService.selectCommentDetailByPage(pageNow, userId);
				entity.setData(page);
				statusCodeEnum = StatusCodeEnum.CODE000000;
		}catch(Exception e){
			log.error(e.getMessage());
			entity.setData(new ArrayList<>());
			statusCodeEnum = StatusCodeEnum.CODE100000;
		}
		
		entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
	}
}
